using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {
	
	public float speed = 0.2f;
	private Vector3 move = Vector3.zero;
	static int ID;
	public int LifePerson;
	public Texture2D LifeBar;
	public float largura;
	public float altura;
	// Use this for initialization
	void Start () {
	LifePerson=100;
		largura=41f;
		altura=105f;
		//ID = int.Parse(transform.name);
		
	}
	
	// Update is called once per frame
	void Update () {
		
		if(networkView.isMine)
		{
			Move();
			
			if(Input.GetKeyDown(KeyCode.Mouse0))
			{
				Shoot();
			}
		}
	}
	 void Move()
	{
		
		if((Input.GetKey("w") && Input.GetKey("d")) || (Input.GetKey("s") && Input.GetKey("d")) || (Input.GetKey("w") && Input.GetKey("a")) || (Input.GetKey("s") && Input.GetKey("a")))
		{
			speed = 0.15f;
		}
		else 
		{
			speed = 0.2f;
		}
		if((Input.GetKey("w") && Input.GetKey("s")) || (Input.GetKey("a") && Input.GetKey("d")))
		{
			speed = 0;
		}
		
		move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
        move = transform.TransformDirection(move);
		move *= speed;
		
		transform.position += move;
	}
	
	void Status()
	{
		
	}
	
	void Shoot()
	{
		Transform tiro = GameObject.Find ("/" + gameObject.name + "/Corpo/Tiro").transform;
		RaycastHit hit;
		
		if(Physics.Raycast(tiro.position, tiro.transform.forward, out hit))
		{
			//Debug.Log(hit.rigidbody.networkView.viewID);
			if(hit.rigidbody.tag == "Player")
			{
				LifePerson=LifePerson-20;
				print(hit.rigidbody.networkView.viewID);
				Destroy(hit.rigidbody.gameObject);
			}
		}
	}
	void OnGUI()
	{
	GUI.DrawTexture(new Rect(Screen.width*0.5f-largura,Screen.height*0.5f-altura,LifePerson,LifeBar.height-20),LifeBar);	
	}
}
